<?php

class Resources_View_List {

    private $resource_type_id;

    function __construct($resource_type_id) {
        $this->resource_type_id = $resource_type_id;
    }

    public function toString() {
        $view_wrapper = new Div_Container('id', 'reservations-wrapper');
        $heading = new Div_Container('id', 'View_Heading');
        $body = new Div_Container('id', 'reservations-body');

        $attrs = array('width' => '680', 'class' => 'gridtable');
        $table = new HTML_Table($attrs);
        $table->setAutoGrow(true);
        $table->setAutoFill('n/a');
        $table->setHeaderContents(0, 0, ' ');
        $table->setHeaderContents(0, 1, ' ');
        $table->setHeaderContents(0, 2, 'ID');
        $table->setHeaderContents(0, 3, 'Name');
        $table->setHeaderContents(0, 4, 'S\N');
        $table->setHeaderContents(0, 5, 'Rent Price');
        $table->setHeaderContents(0, 6, 'Note');
        $table->setHeaderContents(0, 7, ' ');
        
        /*$more_btn = '<button onclick=window.location.href=' . '\'index.php?action=resource_detail&id=11872394\'' .
                        ' type="button" name="more" id="moreResources">more</button>';
         * 
         */
        
        
        $delete_btn = '<input class="delete_btn" type="submit" value="Delete Selected" name="" />';
        $add_btn = '<button onclick=window.location.href=' . '\'index.php?action=add_resource&resource_type_id=' . $this->resource_type_id .  '\'' .
                        ' type="button" name="add" id="addResource">add</button>';;
       
        $resource_inventory = ResourceDB::getAllResourcesByType($this->resource_type_id);
        $data = array();
         if (!is_null($resource_inventory)) {
            $resource_inventory->rewind();
            while ($resource_inventory->valid()) {
                $current_item = $resource_inventory->current();
                $id = $current_item->getResource_id();
                $name = $current_item->getResource_name();
                $s_n = $current_item->getResource_identifier();
                $rent = $current_item->getResource_hour_rent_price();
                $note = $current_item->getResource_note();
                $type_id = $current_item->getResource_type_id();
                
                $checkbox = '<input type="checkbox" name="resource_grp_id[]" value="' . $id . '" />';
                $edit_btn = '<button onclick=window.location.href=' . '\'index.php?action=edit_resource&resource_id=' . $id . '\'' .
                        ' type="button" name="edit" id="editResource">edit</button>';
                
                $table_row_data = array($checkbox, $name, $id, $s_n, $rent, $note, $edit_btn);
                $data[] = $table_row_data;
                $resource_inventory->next();
            }
            for ($nr = 0; $nr < count($data); $nr++) {
            $table->setHeaderContents($nr + 1, 0, (string) $nr + 1);
            for ($i = 0; $i < 7; $i++) {
                if ('' != $data[$nr][$i]) {
                    $table->setCellContents($nr + 1, $i + 1, $data[$nr][$i]);
                }
            }
        }
            
        } else {
            //no product types
        }
      
        $form_head = '<form method="post" id="resources_list_form" action="' . $_SERVER['PHP_SELF'] .'">' .
                '<input type="hidden" name="action" id="action-0" value="resources_list" />' .
                '<input type="hidden" name="resource_type_id" id="action-0" value="' . $this->resource_type_id .'" />';
        
        $body->addContent($add_btn);
        $body->addContent($form_head);
        $body->addContent($table->toHtml());
        $body->addContent($delete_btn);
        $body->addContent("</form>");

        $heading->addContent('<h1>Reservations Page</h1></br></br>');

        $view_wrapper->addContent($heading->toString());
        $view_wrapper->addContent($body->toString());

        return $view_wrapper->toString();
    }

}

?>
